<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="资源编码
规则：
链接：
数据列：
按钮：" prop="code">
      <el-input v-model="dataForm.code" placeholder="资源编码
规则：
链接：
数据列：
按钮："></el-input>
    </el-form-item>
    <el-form-item label="接口名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="接口名称"></el-input>
    </el-form-item>
    <el-form-item label="菜单ID
#c_auth_menu" prop="menuId">
      <el-input v-model="dataForm.menuId" placeholder="菜单ID
#c_auth_menu"></el-input>
    </el-form-item>
    <el-form-item label="" prop="method">
      <el-input v-model="dataForm.method" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="url">
      <el-input v-model="dataForm.url" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="接口描述" prop="describe">
      <el-input v-model="dataForm.describe" placeholder="接口描述"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          code: '',
          name: '',
          menuId: '',
          method: '',
          url: '',
          describe: ''
        },
        dataRule: {
          code: [
            { required: true, message: '资源编码
规则：
链接：
数据列：
按钮：不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '接口名称不能为空', trigger: 'blur' }
          ],
          menuId: [
            { required: true, message: '菜单ID
#c_auth_menu不能为空', trigger: 'blur' }
          ],
          method: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          url: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          describe: [
            { required: true, message: '接口描述不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/auth/authresource/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.code = data.authResource.code
                this.dataForm.name = data.authResource.name
                this.dataForm.menuId = data.authResource.menuId
                this.dataForm.method = data.authResource.method
                this.dataForm.url = data.authResource.url
                this.dataForm.describe = data.authResource.describe
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/auth/authresource/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'code': this.dataForm.code,
                'name': this.dataForm.name,
                'menuId': this.dataForm.menuId,
                'method': this.dataForm.method,
                'url': this.dataForm.url,
                'describe': this.dataForm.describe
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
